package com.example.imtoken.modules.user.create.backups.mnemonic.confirmation

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.example.imtoken.core.App
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.entitys.app.ItemWordsEntity
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.WalletBackUpSchema
import java.util.*

class ConfirmationMnemonicsViewModel : BaseViewModel() {

    private val okWords = ArrayList<ItemWordsEntity>()

    val wordsLiveData = MutableLiveData<MutableList<ItemWordsEntity>>()

    val selectWordsLiveData =
        MutableLiveData<MutableList<ItemWordsEntity>>(ArrayList<ItemWordsEntity>())

    val selectResult = MutableLiveData(false)

    val showErrorMsg = MutableLiveData(false)

    fun initWords(value: String?) {
        val words = ArrayList<ItemWordsEntity>()
        //获取助记词
        var mnemonics = value ?: (//获取助记词
                App.newCreateData?.coin?.mnemonic ?: App.createLiveData.value?.coin?.mnemonic)
        //将助记词分割
        val mList = mnemonics?.split(" ")
        mList?.forEach {
            val itemWordsEntity = ItemWordsEntity(it)
            words.add(itemWordsEntity)
        }
        okWords.addAll(words)
        words.shuffle()
        wordsLiveData.value = words
    }

    fun setSelect(position: Int) {
        val newWords = ArrayList<ItemWordsEntity>()
        wordsLiveData.value?.let {
            for (item in it) {
                newWords.add(item.copy())
            }
            newWords[position].isSelect = !newWords[position].isSelect
            if (newWords[position].isSelect) {
                addSelectWords(newWords[position])
            } else {
                removeSelectWords(newWords[position])
            }
            wordsLiveData.value = newWords
        }
    }

    private fun addSelectWords(itemWordsEntity: ItemWordsEntity) {
        val newWords = ArrayList<ItemWordsEntity>()
        selectWordsLiveData.value?.let {
            for (item in it) {
                newWords.add(item.copy())
            }
            newWords.add(itemWordsEntity)
            for (i in 0 until newWords.size) {
                if (newWords[i].word == okWords[i].word) {
                    newWords[i].isError = false
                } else {
                    newWords[i].isError = true
                    showErrorMsg.value = true
                }
            }
            selectWordsLiveData.value = newWords

            if (newWords.size == okWords.size) {
                var result = true
                for (i in 0 until newWords.size) {
                    if (newWords[i].word != okWords[i].word) {
                        result = false
                        break
                    }
                }
                selectResult.value = result
            } else {
                selectResult.value = false
            }
        }
    }

    private fun removeSelectWords(itemWordsEntity: ItemWordsEntity) {
        val newWords = ArrayList<ItemWordsEntity>()
        selectWordsLiveData.value?.let {
            for (item in it) {
                newWords.add(item.copy())
            }
            var p = -1
            for (i in 0 until newWords.size) {
                if (newWords[i].word == itemWordsEntity.word) {
                    p = i
                }
            }
            if (p != -1) newWords.removeAt(p)

            for (i in 0 until newWords.size) {
                newWords[i].isError = newWords[i].word != okWords[i].word
            }

            selectWordsLiveData.value = newWords

            selectResult.value = false
        }
    }

    fun removeSelectWords2(itemWordsEntity: ItemWordsEntity) {
        removeSelectWords(itemWordsEntity)

        val newWords = ArrayList<ItemWordsEntity>()
        wordsLiveData.value?.let {
            for (item in it) {
                newWords.add(item.copy())
            }
            var p = -1
            for (i in 0 until newWords.size) {
                if (newWords[i].word == itemWordsEntity.word) {
                    p = i
                }
            }
            if (p != -1) newWords[p].isSelect = false
            for (i in 0 until newWords.size) {
                newWords[i].isError = newWords[i].word != okWords[i].word
            }

            wordsLiveData.value = newWords

            selectResult.value = false
        }
    }

    fun addBackUp() {
        val backUp = WalletBackUpSchema(
            App.newCreateData?.coin?.address ?: App.createLiveData.value?.coin?.address, true
        )
        if (AppDataBase.instance.getWalletBackUpDao().insertWalletBackUp(backUp) > 0) {
            LogUtils.e("添加钱包备份信息成功")
        } else {
            LogUtils.e("添加钱包备份信息失败")
        }
    }

}